// 用户管理接口
import request from '@/utils/request'
import type { ResUserRole, UserInfo, ReqSetUserRole, SetUserRole } from './type'
enum API {
  // 获取用户列表
  get_userlist = '/admin/acl/user/',
  // 新增用户
  add_user = '/admin/acl/user/save',
  // 修改用户
  update_user = '/admin/acl/user/update',
  // 全部角色和当前账号的角色
  get_usrrole = '/admin/acl/user/toAssign/',
  // 给已有用户分配角色
  set_userrole = '/admin/acl/user/doAssignRole',
  // 删除指定id的用户
  delete_user_id = '/admin/acl/user/remove/',
  // 批量删除用户
  delete_userlist = '/admin/acl/user/batchRemove',
}

// 获取用户列表
export const reqUserList = (page: number, limit: number, username?: string) => {
  if (!username) return request.get<any, any>(API.get_userlist + `${page}/${limit}`)
  return request.get<any, any>(    API.get_userlist + `${page}/${limit}?username=${username}`)
}

// 添加/修改用户信息
export const reqAddOrUpdateUser = (user: UserInfo) => {
  if (user.id) return request.put<any, any>(API.update_user, user)
  return request.post<any, any>(API.add_user, user)
}

// 获取全部角色和当前用户角色
export const reqGetUserRole = (id: number) =>
  request.get<any, ResUserRole>(API.get_usrrole + id)

// 设置用户角色
export const reqSetUserRole = (data: SetUserRole) =>
  request.post<ReqSetUserRole, any>(API.set_userrole, data)

// 删除指定id的用户
export const reqDeleteUserById = (id: number) =>
  request.delete<any, any>(API.delete_user_id + id)

// 批量删除用户
export const reqDeleteUserList = (list: number[]) =>
  request.delete<any, any>(API.delete_userlist, { data: list })
